<?php

class Core_Services_Locale_VO
{
	/**
	 * The AMF Type mapping 
	 * 
	 * @var string
	 */
    public $_explicitType = 'LocaleVO';
	
	/**
	 * The Unique Database Identifier for this locale
	 *
	 * @var int
	 */
	public $id;
	
	/**
	 * The Locale for this locale.
	 *
	 * @var string
	 */
	public $language;
	
	/**
	 * The Country for this locale.
	 * 
	 * @var string
	 */
	public $country;
	
	/**
	 * Preformatted getter autohandler
	 *
	 * @param string $key
	 * @param * $val
	 * @return *
	 */
	public function __get ( $key )
	{
		switch ( $key )
		{
			case 'isoCode':
				return strtolower( $this->language ) . "_" . strtoupper( $this->country );
			default:
				return null;
		}
	}
	
	/**
	 * Generic setter function.
	 *
	 * @param string $key
	 * @param string $value
	 */
	public function __set ( $key, $value )
	{
		switch ( $key )
		{
			case 'isoCode':
				$this->setIsoCode( $value );
				break;
		}
	}
	
	/**
	 * Private function that sets the value of this value object to an ISO code.
	 *
	 * @param string $isoCode
	 */
	private function setIsoCode ( $isoCode )
	{
		// Check to make sure it's a valid ISO Code
		$model = new Core_Model_Locale();
		

		$timeConfig = Core_Config::read('time');
		
		if ( !$model->isValid ( $isoCode ) )
		{
			$isoCode = $timeConfig->locale;
		}
		
		$parts = split( "_", $value );
		$this->language = strtolower ( $parts[0] );
		$this->country = strtoupper ( $parts[1] );
	}
}